package com.example.streams;

import com.example.beanutils.User;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author hej
 * @date 2024/8/20 15:04
 */
public class StreamMap {
    public static void main(String[] args) {
        //stream 按某字段转为map，value为多个list组合用特殊符号隔开
        List<User> people = Arrays.asList(
                new User("Alice",1, "A"),
                new User("Bob",1, "B"),
                new User("Charlie",1, "A"),
                new User("David",2, "B"),
                new User("Eve",2, "C")
        );

        Map<String, String> result = people.stream()
                .collect(Collectors.groupingBy(
                        g -> g.getGender() + g.getAge(), // 按 gender + age 分组
                        Collectors.mapping(
                                User::getName, // 获取 name
                                Collectors.joining(",") // 用逗号连接
                        )
                ));

        // 输出结果
        result.forEach((key, value) -> System.out.println(key + ": " + value));
    }
}
